﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YiYiTong.Rocket.Helper;

namespace YiYiTong.Rocket.DataAccess
{
    public class MenuDataAccess
    {
        #region 单例模式
        private MenuDataAccess()
        {
        }

        private static MenuDataAccess _instance;
        private static readonly object _syncObject = new object();

        public static MenuDataAccess Instance
        {
            get
            {
                if (_instance == null)
                {
                    lock (_syncObject)
                    {
                        if (_instance == null)
                        {
                            _instance = new MenuDataAccess();
                        }
                    }
                }
                return _instance;
            }
        }
        #endregion 单例模式

        /// <summary>
        /// 添加菜单
        /// </summary>
        /// <param name="menuCode">菜单代码</param>
        /// <param name="menuName">菜单名称</param>
        /// <param name="menuUrl">菜单对应地址</param>
        /// <param name="parentId">菜单父编号,0顶级菜单</param>
        /// <param name="isVisible">是否可见 1不可见 0可见</param>
        /// <param name="createTime">创建时间</param>
        /// <param name="isDelete">是否删除</param>
        /// <param name="remark">备注</param>
        /// <returns>返回是否添加成功</returns>
        public int AddMenu(string menuCode, string menuName, string menuUrl, int parentId, int isVisible, string createTime, int isDelete, string remark)
        {
            int ret = 0;
            string strSql = @"insert into Menu (MenuCode,MenuName,MenuUrl,ParentID,IsVisible,CreateTime,IsDelete,Remark) 
                                values (@MenuCode,@MenuName,@MenuUrl,@ParentID,@IsVisible,@CreateTime,@IsDelete,@Remark)";
            try
            {
                DBHelper db = DBFactory.AdminDB;
                DbCommand Procdbcomm = db.GetSqlStringCommand(strSql);
                db.AddInParameter(Procdbcomm, "@MenuCode", DbType.String, menuCode);
                db.AddInParameter(Procdbcomm, "@MenuName", DbType.String, menuName);
                db.AddInParameter(Procdbcomm, "@MenuUrl", DbType.String, menuUrl);
                db.AddInParameter(Procdbcomm, "@ParentID", DbType.Int32, parentId);
                db.AddInParameter(Procdbcomm, "@IsVisible", DbType.Int32, isVisible);
                db.AddInParameter(Procdbcomm, "@CreateTime", DbType.DateTime, createTime);
                db.AddInParameter(Procdbcomm, "@IsDelete", DbType.Int32, isDelete);
                db.AddInParameter(Procdbcomm, "@Remark", DbType.String, remark);
                ret = db.ExecuteNonQuery(Procdbcomm);
            }
            catch (Exception ex)
            {
                //记录日志
                ret = -99;
                LogHelper.Error("添加菜单-AddMenu" + ex.ToString());
            }
            return ret;
        }

        /// <summary>
        /// 修改菜单
        /// </summary>
        /// <param name="menuId">菜单编号</param>
        /// <param name="menuCode">菜单代码</param>
        /// <param name="menuName">菜单名称</param>
        /// <param name="menuUrl">菜单对应地址</param>
        /// <param name="parentId">菜单父编号,0顶级菜单</param>
        /// <param name="isVisible">是否可见 1不可见 0可见</param>
        /// <param name="remark">备注</param>
        /// <returns>返回是否添加成功</returns>
        public int EditMenu(int menuId, string menuCode, string menuName, string menuUrl, int parentId, int isVisible, string remark)
        {
            int ret = 0;
            string strSql = @"update Menu set MenuCode=@MenuCode,MenuName=@MenuName,MenuUrl=@MenuUrl,
                                ParentID=@ParentID,IsVisible =@IsVisible,Remark=@Remark where MenuID=@MenuID";
            try
            {
                DBHelper db = DBFactory.AdminDB;
                DbCommand Procdbcomm = db.GetSqlStringCommand(strSql);
                db.AddInParameter(Procdbcomm, "@MenuCode", DbType.String, menuCode);
                db.AddInParameter(Procdbcomm, "@MenuName", DbType.String, menuName);
                db.AddInParameter(Procdbcomm, "@MenuUrl", DbType.String, menuUrl);
                db.AddInParameter(Procdbcomm, "@ParentID", DbType.Int32, parentId);
                db.AddInParameter(Procdbcomm, "@IsVisible", DbType.Int32, isVisible);
                db.AddInParameter(Procdbcomm, "@Remark", DbType.String, remark);
                db.AddInParameter(Procdbcomm, "@MenuID", DbType.String, menuId);
                ret = db.ExecuteNonQuery(Procdbcomm);
            }
            catch (Exception ex)
            {
                //记录日志
                ret = -99;
                LogHelper.Error("修改菜单-EditMenu" + ex.ToString());
            }
            return ret;
        }

        /// <summary>
        /// 修改菜单
        /// </summary>
        /// <param name="menuId">菜单编号</param>
        /// <param name="menuCode">菜单代码</param>
        /// <param name="menuUrl">菜单对应地址</param>
        /// <param name="parentId">菜单父编号,0顶级菜单</param>
        /// <param name="isVisible">是否可见 1不可见 0可见</param>
        /// <param name="remark">备注</param>
        /// <returns>返回是否添加成功</returns>
        public int EditMenu(int menuId, string menuCode, string menuUrl, int parentId, int isVisible, string remark)
        {
            int ret = 0;
            string strSql = @"update Menu set MenuCode=@MenuCode,MenuUrl=@MenuUrl,ParentID=@ParentID,
                                IsVisible=@IsVisible,Remark=@Remark where MenuID=@MenuID";
            try
            {
                DBHelper db = DBFactory.AdminDB;
                DbCommand Procdbcomm = db.GetSqlStringCommand(strSql);
                db.AddInParameter(Procdbcomm, "@MenuCode", DbType.String, menuCode);
                db.AddInParameter(Procdbcomm, "@MenuUrl", DbType.String, menuUrl);
                db.AddInParameter(Procdbcomm, "@ParentID", DbType.Int32, parentId);
                db.AddInParameter(Procdbcomm, "@IsVisible", DbType.Int32, isVisible);
                db.AddInParameter(Procdbcomm, "@Remark", DbType.String, remark);
                db.AddInParameter(Procdbcomm, "@MenuID", DbType.Int32, menuId);
                ret = db.ExecuteNonQuery(Procdbcomm);
            }
            catch (Exception ex)
            {
                //记录日志
                ret = -99;
                LogHelper.Error("修改菜单-EditMenu" + ex.ToString());
            }
            return ret;
        }

        /// <summary>
        /// 根据菜单编号获取菜单信息
        /// </summary>
        /// <param name="menuId">菜单编号</param>
        /// <returns></returns>
        public DataTable GetMenuInfo(int menuId)
        {
            DataTable dt = new DataTable();
            string strSql = "select * from Menu where IsDelete=0 and MenuID=@MenuID";

            try
            {
                DBHelper db = DBFactory.AdminDB;
                DbCommand Procdbcomm = db.GetSqlStringCommand(strSql);
                db.AddInParameter(Procdbcomm, "@MenuID", DbType.Int64, menuId);
                var ds = db.ExecuteDataSet(Procdbcomm);
                if (ds != null && ds.Tables[0] != null && ds.Tables[0].Rows.Count != 0)
                {
                    dt = ds.Tables[0];
                }
            }
            catch (Exception ex)
            {
                //记录日志
                LogHelper.Error("根据菜单编号获取菜单信息-GetMenuInfo" + ex.ToString());
            }
            return dt;
        }

        /// <summary>
        /// 根据菜单名称获取菜单信息
        /// </summary>
        /// <param name="menuId">菜单名称</param>
        /// <returns></returns>
        public DataTable GetMenuInfo(string menuName)
        {
            DataTable dt = new DataTable();
            string strSql = "select * from Menu where IsDelete=0 and MenuName=@MenuName";

            try
            {
                DBHelper db = DBFactory.AdminDB;
                DbCommand Procdbcomm = db.GetSqlStringCommand(strSql);
                db.AddInParameter(Procdbcomm, "@MenuName", DbType.String, menuName);
                var ds = db.ExecuteDataSet(Procdbcomm);
                if (ds != null && ds.Tables[0] != null && ds.Tables[0].Rows.Count != 0)
                {
                    dt = ds.Tables[0];
                }
            }
            catch (Exception ex)
            {
                //记录日志
                LogHelper.Error("根据菜单名称获取菜单信息-GetMenuInfo" + ex.ToString());
            }
            return dt;
        }

        /// <summary>
        /// 删除菜单（逻辑删除）
        /// </summary>
        /// <param name="menuId">菜单编号</param>
        /// <returns></returns>
        public int DeleteMenu(int menuId)
        {
            int ret = 0;
            string strSql = "update Menu set IsDelete=1 where MenuID=@MenuID";
            try
            {
                DBHelper db = DBFactory.AdminDB;
                DbCommand Procdbcomm = db.GetSqlStringCommand(strSql);
                db.AddInParameter(Procdbcomm, "@MenuID", DbType.Int32, menuId);
                ret = db.ExecuteNonQuery(Procdbcomm);
            }
            catch (Exception ex)
            {
                //记录日志
                ret = -99;
                LogHelper.Error("删除菜单-DeleteMenu" + ex.ToString());
            }
            return ret;
        }

        /// <summary>
        /// 查询菜单
        /// </summary>
        /// <param name="menuCode">菜单代码</param>
        /// <param name="menuName">菜单名称</param>
        /// <param name="isVisible">是否可见 -10 全部 0可见 1不可见</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageRows">每页行数</param>
        /// <returns></returns>
        public DataSet GetPageList(string menuCode, string menuName, int isVisible, int pageIndex, int pageRows)
        {
            DataSet dsMenu = new DataSet();
            try
            {
                DBHelper db = DBFactory.AdminDB;
                DbCommand Procdbcomm = db.GetStoredProcCommand("proc_menu_getlist");
                db.AddInParameter(Procdbcomm, "@menuCode", DbType.String, menuCode);
                db.AddInParameter(Procdbcomm, "@menuName", DbType.String, menuName);
                db.AddInParameter(Procdbcomm, "@isVisible", DbType.Int32, isVisible);
                db.AddInParameter(Procdbcomm, "@pageIndex", DbType.Int32, pageIndex);
                db.AddInParameter(Procdbcomm, "@pageRows", DbType.Int32, pageRows);
                dsMenu = db.ExecuteDataSet(Procdbcomm);
            }
            catch (Exception ex)
            {
                //记录日志
                LogHelper.Error("查询菜单-GetPageList" + ex.ToString());
            }
            return dsMenu;
        }

        /// <summary>
        /// 获取所有菜单列表
        /// </summary>
        /// <returns></returns>
        public DataTable GetAllMenuList()
        {
            DataTable dt = new DataTable("Menu");
            string strSql = "select * from Menu where IsDelete=0";
            try
            {
                DBHelper db = DBFactory.AdminDB;
                DbCommand Procdbcomm = db.GetSqlStringCommand(strSql);
                var ds = db.ExecuteDataSet(Procdbcomm);
                if (ds != null && ds.Tables[0] != null && ds.Tables[0].Rows.Count != 0)
                {
                    dt = ds.Tables[0];
                }
            }
            catch (Exception ex)
            {
                //记录日志
                LogHelper.Error("获取所有菜单列表-GetAllMenuList" + ex.ToString());
            }
            return dt;
        }
    }
}